package workflow

import (
	"encoding/json"
	"time"
)

// Run represents a workflow run
type Run struct {
	// ID is the run ID
	ID string `json:"id"`
	// WorkflowID is the workflow ID
	WorkflowID string `json:"workflowId"`
	// Status is the run status
	Status string `json:"status"`
	// Input is the workflow input parameters
	Input map[string]interface{} `json:"input,omitempty"`
	// Output is the workflow output
	Output map[string]interface{} `json:"output,omitempty"`
	// StartTime is the start time
	StartTime time.Time `json:"startTime"`
	// EndTime is the end time
	EndTime time.Time `json:"endTime,omitempty"`
	// NodeID is the ID of the node running the workflow
	NodeID string `json:"nodeId,omitempty"`
	// Error is the error message
	Error string `json:"error,omitempty"`
}

// RunStatus represents the status of a workflow run
type RunStatus struct {
	// RunID is the run ID
	RunID string `json:"runId"`
	// WorkflowID is the workflow ID
	WorkflowID string `json:"workflowId"`
	// Status is the run status
	Status string `json:"status"`
	// Progress is the run progress (0-100)
	Progress int `json:"progress"`
	// StartTime is the start time
	StartTime time.Time `json:"startTime"`
	// EndTime is the end time
	EndTime time.Time `json:"endTime,omitempty"`
	// NodeID is the ID of the node running the workflow
	NodeID string `json:"nodeId,omitempty"`
	// NodeStatuses are the statuses of the workflow nodes
	NodeStatuses map[string]string `json:"nodeStatuses,omitempty"`
	// Error is the error message
	Error string `json:"error,omitempty"`
}

// ToJSON converts the Run to JSON
func (r *Run) ToJSON() ([]byte, error) {
	return json.Marshal(r)
}

// FromJSON converts JSON to a Run
func (r *Run) FromJSON(data []byte) error {
	return json.Unmarshal(data, r)
}
